import LoginHeader from '@/components/login-header/index'
import LoginByCode from '@/apis/LoginByCode'
import SendCode from '@/apis/SendCode'
import JobinfoQuery from '@/apis/JobinfoQuery'
export default {
  components: {
    LoginHeader,
  },
  data() {
    return {
      rightCode: '',
      inCode: '',
      CodeText: '获取验证码',
      countDown: 0,
      interval: '',
    }
  },
  methods: {
    backPage() {
      uni.navigateBack({
        delta: 1,
      })
    },
    getCode() {
      if (this.countDown > 0) {
        return
      }
      const _this = this
      uni.getStorage({
        key: 'phoneNumber',
        success: function(res) {
          const phoneNumber = res.data
          // 此处发送验证码请求
          SendCode({
            phone: phoneNumber,
          }).then((res) => {
            if (res.data.flag === true) {
              _this.$toast('发送成功')
              _this.countDown = 59
              _this.interval = setInterval(() => {
                _this.CodeText = _this.countDown
                if (_this.countDown === 0) {
                  _this.CodeText = '获取验证码'
                  clearInterval(_this.interval)
                } else {
                  _this.countDown--
                }
              }, 1000)
            } else {
              _this.$toast('该手机号尚未注册')
              setTimeout(() => {
                _this.backPage()
              }, 600)
            }
          })
        },
      })
    },
    login() {
      // 用户表单输入判断
      if (this.inCode === '') {
        this.$toast('输入的验证码不能为空')
        return
      } else if (this.inCode.length !== 4) {
        this.$toast('请输入正确的验证码')
        return
      }
      const _this = this
      uni.getStorage({
        key: 'phoneNumber',
        success: function(res) {
          const phoneNumber = res.data
          LoginByCode({
            phone: phoneNumber,
            code: _this.inCode,
          }).then((res) => {
            if (res.data.authenticated === true) {
              _this.$toast('登录成功')
              clearInterval(_this.interval)
              // TODO:验证码登录接口的返回值是否正确
              // console.log(res)
              // 处理用户信息
              _this.$store.commit('initUserInfo', res.data.principal)
              _this.$store.commit('initAuthorization', res.header.Authorization)
              _this.$store.commit(
                'initRole',
                res.data.authorities[0].role.split('_')[1]
              )
              // 获取岗位信息
              if (_this.$store.state.userInfo) {
                JobinfoQuery(
                  {
                    jobNumber: _this.$store.state.userInfo.jobNumber,
                  },
                  _this.$store.state.authorization
                ).then((res) => {
                  _this.$store.commit('initPostInfo', res.data.data)
                  // console.log(res)
                  setTimeout(() => {
                    uni.reLaunch({
                      url: '/pages/main/index',
                    })
                  }, 600)
                })
              }
            } else {
              _this.$toast('验证码错误')
              console.log(res)
            }
          })
        },
      })
    },
  },
}
